//
//  CCYWToast.m
//  T-BoxManager
//
//  Created by 车音 on 2019/6/24.
//  Copyright © 2019 CheYin. All rights reserved.
//

#import "CYWToast.h"

#if !__has_feature(objc_arc)
#error WToast requires ARC
#endif

#define TABBAR_OFFSET 44.0f
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@interface CYWToast()

@property (nonatomic) NSInteger duration;
@property (nonatomic) BOOL roundedCorners;

@end


@implementation CYWToast

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame]) != nil) {
        _duration = DDPkWTShort;
        _roundedCorners = NO;
        self.userInteractionEnabled = NO;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillAnimateRotation) name:@"UIWindowWillAnimateRotationNotification" object:nil];
    }
    return self;
}

- (void)windowWillAnimateRotation {
    [self __flipViewAccordingToStatusBarOrientation];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)__show {
    __weak typeof(self) weakSelf = self;
    __block NSInteger bDuration = _duration;
    
    [UIView
     animateWithDuration:0.2f
     animations:^{
         weakSelf.alpha = 1.0f;
     }
     completion:^(BOOL finished) {
         [weakSelf performSelector:@selector(__hide) withObject:nil afterDelay:bDuration];
     }];
}

- (void)__hide {
    __weak typeof(self) weakSelf = self;
    
    [UIView
     animateWithDuration:0.8f
     animations:^{
         weakSelf.alpha = 0.0f;
     }
     completion:^(BOOL finished) {
         [weakSelf removeFromSuperview];
     }];
}

- (void)setRoundedCorners:(BOOL)roundedCorners {
    _roundedCorners = roundedCorners;
    
    if (_roundedCorners) {
        CALayer *layer = self.layer;
        layer.masksToBounds = YES;
        layer.cornerRadius = 5.0f;
    }
}

+ (CYWToast *)__createWithText:(NSString *)text {
    CGFloat screenWidth;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        screenWidth = screenSize.width;
    }
    else {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        
        switch (orientation) {
            case UIInterfaceOrientationPortraitUpsideDown: {
                screenWidth = MIN(screenSize.width, screenSize.height);
                break;
            }
            case UIInterfaceOrientationLandscapeLeft: {
                screenWidth = MAX(screenSize.width, screenSize.height);
                break;
            }
            case UIInterfaceOrientationLandscapeRight: {
                screenWidth = MAX(screenSize.width, screenSize.height);
                break;
            }
            default: {
                screenWidth = MIN(screenSize.width, screenSize.height);
                break;
            }
        }
    }
    
    CGFloat x = screenWidth * 0.2;
    CGFloat width = screenWidth - x * 2.0f;
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = RGB(255, 255, 255);
    textLabel.numberOfLines = 0;
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    CGRect frame = CGRectZero;
    CGSize sizeConstraint = CGSizeMake(width - 20.0f, FLT_MAX);
    
    frame = [text boundingRectWithSize:sizeConstraint
                               options:NSStringDrawingUsesLineFragmentOrigin
                            attributes:@{NSFontAttributeName: textLabel.font}
                               context:nil];
    
    frame.size.width = width;
    frame.size.height = MAX(frame.size.height + 15.0f, 33.0f);
    
    CYWToast *toast = [[CYWToast alloc] initWithFrame:frame];
    toast.backgroundColor = RGBA(0, 0, 0, 0.8f);
    toast.layer.cornerRadius = 10;
    
    textLabel.text = text;
    frame.origin.x = floor((toast.frame.size.width - frame.size.width) / 2.0f);
    frame.origin.y = floor((toast.frame.size.height - frame.size.height) / 2.0f);
    textLabel.frame = frame;
    
    [toast addSubview:textLabel];
    
    toast.alpha = 0.0f;
    
    return toast;
}

+ (CYWToast *)__createWithImage:(UIImage *)image {
    CGFloat screenWidth;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        screenWidth = screenSize.width;
    }
    else {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        
        switch (orientation) {
            case UIInterfaceOrientationPortraitUpsideDown: {
                screenWidth = MIN(screenSize.width, screenSize.height);
                break;
            }
            case UIInterfaceOrientationLandscapeLeft: {
                screenWidth = MAX(screenSize.width, screenSize.height);
                break;
            }
            case UIInterfaceOrientationLandscapeRight: {
                screenWidth = MAX(screenSize.width, screenSize.height);
                break;
            }
            default: {
                screenWidth = MIN(screenSize.width, screenSize.height);
                break;
            }
        }
    }
    
    
    CGFloat x = screenWidth * 0.2;
    CGFloat width = screenWidth - x * 2.0f;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CGSize sz = imageView.frame.size;
    
    CGRect frame = CGRectZero;
    frame.size.width = width;
    frame.size.height = MAX(sz.height + 20.0f, 38.0f);
    
    CYWToast *toast = [[CYWToast alloc] initWithFrame:frame];
    toast.backgroundColor = RGBA(0, 0, 0, 0.8f);
    toast.layer.cornerRadius = 10;
    
    frame.origin.x = floor((toast.frame.size.width - sz.width) / 2.0f);
    frame.origin.y = floor((toast.frame.size.height - sz.height) / 2.0f);
    frame.size = sz;
    imageView.frame = frame;
    [toast addSubview:imageView];
    
    toast.alpha = 0.0f;
    
    return toast;
}

- (void)__flipViewAccordingToStatusBarOrientation {
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat x;
    CGFloat y;
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        x = floor((screenSize.width - self.bounds.size.width) / 2.0f);
        
        if ([[UIApplication sharedApplication] statusBarFrame].size.height > 30) {
            y = ScreenHeight - self.bounds.size.height - 119;
        } else {
            y = ScreenHeight - self.bounds.size.height - 119 + 34;
        }
//        y =  ScreenHeight / 2 + 80.f;

    }
    else {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        CGFloat angle = 0.0;
        
        switch (orientation) {
            case UIInterfaceOrientationPortraitUpsideDown: {
                angle = M_PI;
                CGFloat screenWidth = MIN(screenSize.width, screenSize.height);
                x = floor((screenWidth - self.bounds.size.width) / 2.0f);
                y = 25.0f + TABBAR_OFFSET;
                break;
            }
            case UIInterfaceOrientationLandscapeLeft: {
                angle = - M_PI / 2.0f;
                CGFloat screenWidth = MAX(screenSize.width, screenSize.height);
                CGFloat screenHeight = MIN(screenSize.width, screenSize.height);
                x = screenHeight - self.bounds.size.height - 15.0f - TABBAR_OFFSET;
                y = floor((screenWidth - self.bounds.size.width) / 2.0f);
                break;
            }
            case UIInterfaceOrientationLandscapeRight: {
                angle = M_PI / 2.0f;
                CGFloat screenWidth = MAX(screenSize.width, screenSize.height);
                x = 15.0f + TABBAR_OFFSET;
                y = floor((screenWidth - self.bounds.size.width) / 2.0f);
                break;
            }
            default: {
                angle = 0.0;
                CGFloat screenWidth = MIN(screenSize.width, screenSize.height);
                CGFloat screenHeight = MAX(screenSize.width, screenSize.height);
                x = floor((screenWidth - self.bounds.size.width) / 2.0f);
                y = screenHeight - self.bounds.size.height - 15.0f - TABBAR_OFFSET;
                break;
            }
        }
        
        self.transform = CGAffineTransformMakeRotation(angle);
    }
    
    CGRect f = self.bounds;
    f.origin = CGPointMake(x, y);
    self.frame = f;
}

/**
 * Show toast with text in application window
 * @param text Text to print in toast window
 */
+ (void)ddpShowWithText:(NSString *)text {
    [CYWToast ddpShowWithText:text duration:DDPkWTShort roundedCorners:NO];
}

/**
 * Show toast with image in application window
 * @param image Image to show in toast window
 */
+ (void)ddpShowWithImage:(UIImage *)image {
    [CYWToast ddpShowWithImage:image duration:DDPkWTShort roundedCorners:NO];
}

/**
 * Show toast with text in application window
 * @param text Text to print in toast window
 * @param duration Toast visibility duration
 */
+ (void)ddpShowWithText:(NSString *)text duration:(NSInteger)duration {
    [CYWToast ddpShowWithText:text duration:duration roundedCorners:NO];
}

/**
 * Show toast with image in application window
 * @param image Image to show in toast window
 * @param duration Toast visibility duration
 */
+ (void)ddpShowWithImage:(UIImage *)image duration:(NSInteger)duration {
    [CYWToast ddpShowWithImage:image duration:DDPkWTShort roundedCorners:NO];
}

/**
 * Show toast with text in application window
 * @param text Image to show in toast window
 * @param duration Toast visibility duration
 * @param roundedCorners Make corners of toast rounded
 */
+ (void)ddpShowWithText:(NSString *)text duration:(NSInteger)duration roundedCorners:(BOOL)roundedCorners {
	CYWToast *toast = [CYWToast __createWithText:text];
	toast.duration = duration;
	toast.roundedCorners = roundedCorners;
	
    UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
	[mainWindow addSubview:toast];
	
	[toast __flipViewAccordingToStatusBarOrientation];
	[toast __show];
}

/**
 * Show toast with image in application window
 * @param image Image to show in toast window
 * @param duration Toast visibility duration
 * @param roundedCorners Make corners of toast rounded
 */
+ (void)ddpShowWithImage:(UIImage *)image duration:(NSInteger)duration roundedCorners:(BOOL)roundedCorners {
	CYWToast *toast = [CYWToast __createWithImage:image];
	toast.duration = duration;
	toast.roundedCorners = roundedCorners;

    UIWindow *mainWindow = nil;
    if ([[CYUtils systemVersion] floatValue] >= 11) {
        mainWindow = [[UIApplication sharedApplication].windows firstObject];
    } else {
        mainWindow = [[UIApplication sharedApplication].windows lastObject];
    }
    [mainWindow addSubview:toast];
    
    [toast __flipViewAccordingToStatusBarOrientation];
    [toast __show];
}

+ (void)ddpShowWithImage:(UIImage *)image text:(NSString *)text{
    CYWToast *toast = [CYWToast __createWithImage:image __createWithText:text];
    
    UIWindow *mainWindow = nil;
    if ([[CYUtils systemVersion] floatValue] >= 11) {
        mainWindow = [[UIApplication sharedApplication].windows firstObject];
    } else {
        mainWindow = [[UIApplication sharedApplication].windows lastObject];
    }
    [mainWindow addSubview:toast];
    
    [toast __flipViewAccordingToStatusBarOrientation];
    [toast __show];
}

+ (void)ddpShowWithImage:(UIImage *)image text:(NSString *)text duration:(NSInteger)duration
{
    CYWToast *toast = [CYWToast __createWithImage:image __createWithText:text];
    toast.duration = duration;
    
    UIWindow *mainWindow = nil;
    if ([[CYUtils systemVersion] floatValue] >= 11) {
        mainWindow = [[UIApplication sharedApplication].windows firstObject];
    } else {
        mainWindow = [[UIApplication sharedApplication].windows lastObject];
    }
    [mainWindow addSubview:toast];
    
    [toast __flipViewAccordingToStatusBarOrientation];
    [toast __show];
}

+ (CYWToast *)__createWithImage:(UIImage *)image __createWithText:(NSString *)text {
    
    if (image) {
        CGFloat screenWidth;
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        
        
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
            screenWidth = screenSize.width;
        }
        else {
            UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
            
            switch (orientation) {
                case UIInterfaceOrientationPortraitUpsideDown: {
                    screenWidth = MIN(screenSize.width, screenSize.height);
                    break;
                }
                case UIInterfaceOrientationLandscapeLeft: {
                    screenWidth = MAX(screenSize.width, screenSize.height);
                    break;
                }
                case UIInterfaceOrientationLandscapeRight: {
                    screenWidth = MAX(screenSize.width, screenSize.height);
                    break;
                }
                default: {
                    screenWidth = MIN(screenSize.width, screenSize.height);
                    break;
                }
            }
        }
        CGFloat x = screenWidth * 0.2;
        CGFloat width = screenWidth - x * 2.0f;
 
        
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.textColor = RGB(255, 255, 255);
        textLabel.numberOfLines = 0;
        textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        CGRect frame = CGRectZero;
        CGSize sizeConstraint = CGSizeMake(width - 20.0f, FLT_MAX);
        
        frame = [text boundingRectWithSize:sizeConstraint
                                   options:NSStringDrawingUsesLineFragmentOrigin
                                attributes:@{NSFontAttributeName: textLabel.font}
                                   context:nil];
        frame.size.width = width;
        frame.size.height = MAX(frame.size.height + 15.0f, 33.0f);
        
        CYWToast *toast = [[CYWToast alloc] initWithFrame:frame];
        toast.backgroundColor = RGBA(0, 0, 0, 0.8f);
        toast.layer.cornerRadius = 10;
        
        frame.origin.x = floor((toast.frame.size.width - frame.size.width) / 2.0f);
        frame.origin.y = floor((toast.frame.size.height - frame.size.height) / 2.0f);
        textLabel.frame = frame;
        
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",text]];
        
        
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        
        attch.image = image;
        
        attch.bounds = CGRectMake(0, -4, 16, 16);
        
        [NSAttributedString attributedStringWithAttachment:attch];
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
        [attri insertAttributedString:string atIndex:0];
        textLabel.attributedText = attri;
        
        [toast addSubview:textLabel];
        
        toast.alpha = 0.0f;
        
        return toast;
    } else {
        return [CYWToast __createWithText:text];
    }
}

@end
